package com.smartscreen.schedulemanage.controller;

import com.smartscreen.common.response.ApiResponse;
import com.smartscreen.schedulemanage.dto.CreateCalendarEventRequest;
import com.smartscreen.schedulemanage.entity.CalendarEvent;
import com.smartscreen.schedulemanage.service.CalendarEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/schedule")
public class CalendarController {
    
    @Autowired
    private CalendarEventService calendarEventService;
    
    @PostMapping("/calendar/events")
    public ApiResponse<Map<String, String>> createCalendarEvent(
            @RequestBody CreateCalendarEventRequest request) {
        String eventId = calendarEventService.createCalendarEvent(request);
        Map<String, String> data = new HashMap<>();
        data.put("eventId", eventId);
        return ApiResponse.created("事件添加成功", data);
    }
    
    @GetMapping("/calendar/events")
    public ApiResponse<List<CalendarEvent>> getCalendarEvents(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        List<CalendarEvent> events = calendarEventService.getCalendarEvents(start, end);
        return ApiResponse.success(events);
    }
    
    @PutMapping("/calendar/events/{eventId}")
    public ApiResponse<Void> updateCalendarEvent(
            @PathVariable String eventId,
            @RequestBody CreateCalendarEventRequest request) {
        calendarEventService.updateCalendarEvent(eventId, request);
        return new ApiResponse<>(200, "事件更新成功");
    }
    
    @DeleteMapping("/calendar/events/{eventId}")
    public ApiResponse<Void> deleteCalendarEvent(@PathVariable String eventId) {
        calendarEventService.deleteCalendarEvent(eventId);
        return new ApiResponse<>(204, "事件删除成功");
    }
}